package com.example.sunmiv1.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.CollectingSilverActivity;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.entity.LoginEntity;
import com.example.sunmiv1.utils.MathUtil;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.views.SlideLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.sunmiv1.activity.CollectingSilverActivity.infoBean;
import static com.example.sunmiv1.activity.CollectingSilverActivity.isVip;

public class CollectingSilverSelectedAdapter extends BaseAdapter {

    private Context context;
    private List<GoodBean.DataBean.GoodsBean> list;
    ViewHolder viewHolder;
    private Util util;
    private ACache aCache;
    private Set<SlideLayout> sets = new HashSet();
    private LoginEntity loginEntity;
    private HashMap<String,TextWatcher> mTextMap=new HashMap<>();
    //會員折扣
    private double vip_discount=1;

    public CollectingSilverSelectedAdapter(Context context, List<GoodBean.DataBean.GoodsBean> list, LoginEntity loginEntity) {
        this.context = context;
        this.list = list;
        aCache = ACache.get(context);
        this.loginEntity = loginEntity;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        position = list.size() - 1 - position;
//        if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.collecting_silver_selected_adapter, null);
        AutoUtils.auto(convertView);
        viewHolder = new ViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        if (list.get(position).getSku_discount() != 1 && list.get(position).getSku_discount() > 0) {
            //viewHolder.collectingSilverSelectedAdapterDiscount.removeTextChangedListener(textWatcher);
            viewHolder.collectingSilverSelectedAdapterDiscount.setText(list.get(position).getSku_discount() + "");
        }
        if ("1".equals(list.get(position).getSerial_number_state())) {
            viewHolder.collectingSilverSelectedSerial.setVisibility(View.VISIBLE);
        } else {
            viewHolder.collectingSilverSelectedSerial.setVisibility(View.GONE);
        }
        if (0 == list.get(position).getIs_gift_good()) {
            if (list.get(position).getSkujson() != null && list.get(position).getSkujson().size() > 0) {
                viewHolder.collectingSilverSelectedAdapterName.setText(list.get(position).getGoods_name() + "(" + list.get(position).getSkujson().get(0).getSku() + ")");
            } else {
                viewHolder.collectingSilverSelectedAdapterName.setText(list.get(position).getGoods_name());
            }
            viewHolder.collectingSilverSelectedAdapterCostPrice.setVisibility(View.GONE);
        } else {
            viewHolder.collectingSilverSelectedAdapterName.setText(list.get(position).getGoods_name() +
                    (TextUtils.isEmpty(list.get(position).getSku_name()) ? "" : "(" + list.get(position).getSku_name() + ")"));
            viewHolder.collectingSilverSelectedAdapterCostPrice.setText("$" + list.get(position).getCost_price());
            viewHolder.collectingSilverSelectedAdapterCostPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            viewHolder.collectingSilverSelectedAdapterCostPrice.setVisibility(View.VISIBLE);
        }


        /**
         * 計算價格
         * */


        String originalPrice = "0";
        double temp_price = 0;
        double price = 0;
        int integral = 0;
        GoodBean.DataBean.GoodsBean goods = list.get(position);
        if (0 == goods.getIs_gift_good()) {
            if (!"".equals(goods.getCheng_price())){
                if (isVip == 1) {
                    price=getVipPrice(1,goods,goods.getCheng_price());
                    originalPrice = goods.getCheng_price();
                }else {
                    price=getVipPrice(0,goods,goods.getCheng_price());
                    originalPrice = goods.getCheng_price();
                }
            }else {
                if (isVip == 1) {
                    if ("2".equals(infoBean.getPlus_type()) && "1".equals(goods.getPlus_mem_status())) { //plus會員
                        if (isSkuGood(position)) { //判斷規格商品
                            if (!TextUtils.isEmpty(goods.getSkujson().get(0).getPlus_price())) {
                                price = getVipPrice(1, goods, goods.getSkujson().get(0).getPlus_price());
                                originalPrice = goods.getSkujson().get(0).getPlus_price();
                                if (!TextUtils.isEmpty(goods.getSkujson().get(0).getPlus_mem_integral())) {
                                    integral = Integer.parseInt(goods.getSkujson().get(0).getPlus_mem_integral());
                                }
                            } else {
                                price = getVipPrice(1, goods, goods.getSkujson().get(0).getRetail_price());
                                originalPrice = goods.getSkujson().get(0).getRetail_price();
                            }
                        } else {
                            price = getVipPrice(1, goods, goods.getPlus_price());
                            originalPrice = goods.getPlus_price();
                            if (!TextUtils.isEmpty(goods.getPlus_mem_integral())) {
                                integral = Integer.parseInt(goods.getPlus_mem_integral());
                            }
                        }
                    } else { //常規會員
                        if ("1".equals(goods.getVip_retail_state())) {
                            if (isSkuGood(position)) {
                                if ("2".equals(goods.getSkujson().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (goods.getSkujson().get(0).getStore_mem_price() != null &&
                                            !TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkujson().get(0).getStore_mem_price());
                                        originalPrice = goods.getSkujson().get(0).getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getSkujson().get(0).getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getStore_mem_price());
                                        originalPrice = goods.getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            }
                        } else {
                            if (isSkuGood(position)) {
                                if ("2".equals(goods.getSkujson().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (goods.getSkujson().get(0).getStore_mem_price() != null &&
                                            !TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkujson().get(0).getStore_mem_price());
                                        originalPrice = goods.getSkujson().get(0).getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getSkujson().get(0).getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = Double.parseDouble(goods.getStore_mem_price());
                                        originalPrice = price + "";
                                        price = getVipPrice(1, goods, price + "");
                                        if (!TextUtils.isEmpty(goods.getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (isSkuGood(position)) {
                        if (goods.getSkujson().get(0).getRetail_price() != null &&
                                !TextUtils.isEmpty(goods.getSkujson().get(0).getRetail_price())) {
                            price = getVipPrice(0, goods, goods.getSkujson().get(0).getRetail_price());
                            originalPrice = goods.getSkujson().get(0).getRetail_price();
                        } else {
                            price = getVipPrice(0, goods, goods.getRetail_price());
                            originalPrice = goods.getRetail_price();
                        }
                    } else {
                        price = getVipPrice(0, goods, goods.getRetail_price());
                        originalPrice = goods.getRetail_price();
                    }
                }
            }
        }
        if ("1".equals(aCache.getAsString("marketing_methods"))|| "0".equals(loginEntity.getData().getMerchantConfig().getIntegral_arrive_cash())) {
            integral = 0;
        }
        if (0 == goods.getIs_gift_good()) {
            if (((isVip == 1 &&"1".equals(goods.getVip_retail_state())&&(!"1".equals(goods.getPlus_mem_status()) || !"2".equals(infoBean.getPlus_type())))
                    ||(isVip == 1 &&"1".equals(goods.getVip_retail_state())&&!"".equals(goods.getCheng_price())))&&infoBean!=null) {
                vip_discount=Double.parseDouble(CollectingSilverActivity.vipDiscount);
                if (vip_discount!=1) {
                    viewHolder.collectingSilverSelectedAdapterVipDiscount.setVisibility(View.VISIBLE);
                    viewHolder.collectingSilverSelectedAdapterVipDiscount.setText(context.getString(R.string.collectingAdapter10) + infoBean.getDiscount() + ")");
                }
            } else {
                viewHolder.collectingSilverSelectedAdapterVipDiscount.setVisibility(View.GONE);
            }
            viewHolder.collectingSilverSelectedAdapterGive.setVisibility(View.GONE);
        } else {
            viewHolder.collectingSilverSelectedAdapterDiscount.setVisibility(View.GONE);
            viewHolder.collectingSilverSelectedAdapterVipDiscount.setVisibility(View.GONE);
            viewHolder.collectingSilverSelectedAdapterCouponTip.setText("");
            viewHolder.collectingSilverSelectedAdapterGive.setVisibility(View.VISIBLE);
        }
//        mVipDiscountMoney += MathUtil.sub(doubleAndMul(originalPrice, goods.getAgoods_count() + ""), doubleAndMul(price + "", goods.getAgoods_count() + ""));
//        if ("1".equals(loginEntity.getData().getCode_price_state())){
//            holder.orderSheetChoosePrice.setVisibility(View.INVISIBLE);
//        }else{
//            holder.orderSheetChoosePrice.setVisibility(View.VISIBLE);
//        }
        final int index = position;
        mTextMap.remove(goods.getId());
        TextWatcher countText=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        mTextMap.put(goods.getId(),countText);
        viewHolder.collectingSilverSelectedAdapterNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(list.get(index).getSerial_number_state())) {
                    Toast.makeText(context, "串碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                    return;
                }else if (!"".equals(list.get(index).getCheng_price())){
                    Toast.makeText(context, "條碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                    return;
                }
                util.util(index, 6);
            }
        });
        if ("".equals(list.get(index).getCheng_price())){
            if (integral > 0 && "2".equals(aCache.getAsString("marketing_methods"))) {
                viewHolder.collectingSilverSelectedAdapterPrice.setText("$" + originalPrice + "+" + integral + context.getString(R.string.collectingAdapter1));
            } else {
                viewHolder.collectingSilverSelectedAdapterPrice.setText("$" + originalPrice);
                if ("1".equals(list.get(position).getPricing_method())) {
                    viewHolder.collectingSilverSelectedAdapterPrice.setText("$" + list.get(position).getRetail_price());
                } else {
                    viewHolder.collectingSilverSelectedAdapterPrice.setText("$" + originalPrice + "/" + list.get(position).getCompany());
                }
            }
            if ("1".equals(list.get(position).getPricing_method())) {
                viewHolder.collectingSilverSelectedAdapterNum.setText(String.valueOf(list.get(position).getAgoods_count()));
            } else {
                viewHolder.collectingSilverSelectedAdapterNum.setText(String.valueOf(list.get(position).getAgoods_count()));
            }
            integral = (int) MathUtil.mul(integral, goods.getAgoods_count());
            goods.setTemporaryIntegral(integral);
            goods.setTemporaryPrice(originalPrice);
            if (integral > 0 && "2".equals(aCache.getAsString("marketing_methods"))) {
                viewHolder.collectingSilverSelectedAdapterTotal.setText(context.getString(R.string.collectingAdapter2) + MathUtil.mul(price, goods.getAgoods_count()) + "+" + integral + context.getString(R.string.collectingAdapter3));
            } else {
                viewHolder.collectingSilverSelectedAdapterTotal.setText(context.getString(R.string.collectingAdapter2) + MathUtil.mul(price, goods.getAgoods_count()));
            }
            if (isEqual("1", aCache.getAsString("manual_discount"))) {
                viewHolder.collectingSilverSelectedAdapterDiscount.setVisibility(View.VISIBLE);
                double dis = list.get(position).getSku_discount();
                String tem = list.get(position).getTemporaryPrice();
                Log.e("TAG", "getView: tem===" + tem);
                double p1 = doubleAndMul(tem, "" + goods.getAgoods_count());
                double p2 = MathUtil.mul(doubleAndMul(tem, dis + ""), goods.getAgoods_count());
                if (dis < Double.parseDouble(aCache.getAsString("manual_min_discount")) || dis > 1 ||
                        ((doubleAndMul(aCache.getAsString("manual_max_amount"), goods.getAgoods_count() + "") < MathUtil.sub(p1, p2)) || dis > 1)) {
                    viewHolder.collectingSilverSelectedAdapterTip.setText(R.string.collectingAdapter4);
//                aCache.put("placeAnOrder", "0");//是否可以下單 1:可以 0:不可以
                    viewHolder.collectingSilverSelectedAdapterCouponTip.setText(context.getString(R.string.collectingAdapter5) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter6) +
                            doubleAndMul(aCache.getAsString("manual_max_amount"), goods.getAgoods_count() + ""));
                    double p3 = MathUtil.mul(getVipPrice(isVip, goods, tem),
                            goods.getAgoods_count());
                    judgeIntegralAndShow(p3, position, viewHolder.collectingSilverSelectedAdapterTotal);
                } else {
                    viewHolder.collectingSilverSelectedAdapterTip.setText("");
//                aCache.put("placeAnOrder", "1");//是否可以下單 1:可以 0:不可以
                    if (dis == 1) {
                        viewHolder.collectingSilverSelectedAdapterCouponTip.setText(context.getString(R.string.collectingAdapter7) +
                                doubleAndMul(aCache.getAsString("manual_max_amount"), goods.getAgoods_count() + ""));
                        double p3 = MathUtil.mul(getVipPrice(isVip, goods, tem),
                                goods.getAgoods_count());
                        judgeIntegralAndShow(p3, position, viewHolder.collectingSilverSelectedAdapterTotal);
                    } else {
                        double p3 = MathUtil.mul(getVipPrice(isVip, goods, tem),
                                goods.getAgoods_count());
                        judgeIntegralAndShow(p3, position, viewHolder.collectingSilverSelectedAdapterTotal);
                        viewHolder.collectingSilverSelectedAdapterCouponTip.setText(context.getString(R.string.collectingAdapter8) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter9) +
                                doubleAndMul(aCache.getAsString("manual_max_amount"), goods.getAgoods_count() + ""));
                    }
                }
            } else {
                viewHolder.collectingSilverSelectedAdapterDiscount.setVisibility(View.GONE);
                viewHolder.collectingSilverSelectedAdapterCouponTip.setText("");
            }
        }else {
            viewHolder.collectingSilverSelectedAdapterPrice.setText("$" + MathUtil.mul(Double.parseDouble(list.get(index).getCheng_price()),vip_discount));
            viewHolder.collectingSilverSelectedAdapterNum.setText(String.valueOf(list.get(position).getAgoods_count()));
            goods.setTemporaryPrice(MathUtil.mul(Double.parseDouble(originalPrice),vip_discount)+"");
            if (isEqual("1", aCache.getAsString("manual_discount"))) {
                viewHolder.collectingSilverSelectedAdapterDiscount.setVisibility(View.VISIBLE);
                double dis = list.get(position).getSku_discount();
                String tem = list.get(position).getTemporaryPrice();
                double p1 = Double.parseDouble(tem);
                double p2 = doubleAndMul(tem, dis + "");
                if (dis < Double.parseDouble(aCache.getAsString("manual_min_discount")) || dis > 1 ||
                        (((Double.parseDouble(aCache.getAsString("manual_max_amount")) < MathUtil.sub(p1, p2)) && Double.parseDouble(aCache.getAsString("manual_max_amount")) != 0) || dis > 1)) {
                    viewHolder.collectingSilverSelectedAdapterTip.setText("折扣錯誤，請重新設置");
//                aCache.put("placeAnOrder", "0");//是否可以下單 1:可以 0:不可以
                    viewHolder.collectingSilverSelectedAdapterCouponTip.setText("當前已優惠" + MathUtil.sub(p1, p2) + ",最多可優惠" +
                            aCache.getAsString("manual_max_amount"));
                    double p3 = Double.parseDouble(tem);
                    setAllPriceText(viewHolder.collectingSilverSelectedAdapterTotal, "小計:$" + p3);
                } else {
                    viewHolder.collectingSilverSelectedAdapterTip.setText("");
//                aCache.put("placeAnOrder", "1");//是否可以下單 1:可以 0:不可以
                    if (dis == 1) {
                        if (Double.parseDouble(aCache.getAsString("manual_max_amount")) != 0) {
                            viewHolder.collectingSilverSelectedAdapterCouponTip.setText("當前已優惠0,最多可優惠" +
                                    aCache.getAsString("manual_max_amount"));
                        } else {
                            viewHolder.collectingSilverSelectedAdapterCouponTip.setText("當前已優惠0");
                        }
                        double p3 = Double.parseDouble(tem);
                        setAllPriceText(viewHolder.collectingSilverSelectedAdapterTotal, "小計:$" + p3);
                    } else {
                        double p3 = Double.parseDouble(tem);
                        setAllPriceText(viewHolder.collectingSilverSelectedAdapterTotal, "小計:$" + p3);
                        if (Double.parseDouble(aCache.getAsString("manual_max_amount")) != 0) {
                            viewHolder.collectingSilverSelectedAdapterCouponTip.setText("當前已優惠" + MathUtil.sub(p1, p2) + ",最多可優惠" +
                                    aCache.getAsString("manual_max_amount"));
                        } else {
                            viewHolder.collectingSilverSelectedAdapterCouponTip.setText("當前已優惠" + MathUtil.sub(p1, p2));
                        }
                    }
                }
            } else {
                viewHolder.collectingSilverSelectedAdapterDiscount.setVisibility(View.GONE);
                viewHolder.collectingSilverSelectedAdapterCouponTip.setText("");
            }
        }
        final TextView discount = viewHolder.collectingSilverSelectedAdapterDiscount;
        final TextView tip = viewHolder.collectingSilverSelectedAdapterTip;
        final TextView coupon = viewHolder.collectingSilverSelectedAdapterCouponTip;
        final TextView tAllPrice = viewHolder.collectingSilverSelectedAdapterTotal;
        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(".")) {
                    if (-1 != charSequence.toString().indexOf(".", charSequence.toString().indexOf(".") + 1)) {
                        discount.setText(charSequence.toString().
                                substring(0, charSequence.toString().length() - 1));
                        //discount.setSelection(charSequence.length() - 1);
                    } else {
                        if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 2) {
                            charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 3);
                            discount.setText(charSequence);
                            //discount.setSelection(charSequence.length());
                        }
                    }
                }
                if (isEqual(".", charSequence.toString().trim().substring(0))) {
                    charSequence = "0" + charSequence;
                    discount.setText(charSequence);
                    //discount.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!isEqual(".", charSequence.toString().substring(1, 2))) {
                        discount.setText(charSequence.subSequence(1, 2));
                        //discount.setSelection(1);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String dis = discount.getText().toString().trim();
                String tem = list.get(index).getTemporaryPrice();
                if (!TextUtils.isEmpty(dis) &&
                        Double.parseDouble(dis) > 0) {
                    list.get(index).setSku_discount(Double.parseDouble(dis));
                }
                if (TextUtils.isEmpty(dis)) {
                    list.get(index).setSku_discount(1);
                }
                boolean a = list.get(index).getSku_discount() < Double.parseDouble(aCache.getAsString("manual_min_discount")) ||
                        list.get(index).getSku_discount() > 1;
                double p1 = doubleAndMul(tem, "" + list.get(index).getAgoods_count());
                double p2 = MathUtil.mul(doubleAndMul(tem, list.get(index).getSku_discount() + ""), list.get(index).getAgoods_count());
                if (a || MathUtil.sub(p1, p2) > doubleAndMul(aCache.getAsString("manual_max_amount"), list.get(index).getAgoods_count() + "")) {
                    tip.setText(R.string.collectingAdapter11);
//                    aCache.put("placeAnOrder", "0");//是否可以下單 1:可以 0:不可以

                    coupon.setText(context.getString(R.string.collectingAdapter12) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter13) +
                            doubleAndMul(aCache.getAsString("manual_max_amount"), list.get(index).getAgoods_count() + ""));
                    double p3 = MathUtil.mul(getVipPrice(isVip, list.get(index), tem),
                            list.get(index).getAgoods_count());
                    judgeIntegralAndShow(p3, index, tAllPrice);
                } else {
                    tip.setText("");
//                    aCache.put("placeAnOrder", "1");//是否可以下單 1:可以 0:不可以
                    if (TextUtils.isEmpty(dis)) {
                        coupon.setText(context.getString(R.string.collectingAdapter14) +
                                doubleAndMul(aCache.getAsString("manual_max_amount"), list.get(index).getAgoods_count() + ""));
                        double p3 = MathUtil.mul(getVipPrice(isVip, list.get(index), tem),
                                list.get(index).getAgoods_count());
                        judgeIntegralAndShow(p3, index, tAllPrice);
                    } else {
                        double p3 = MathUtil.mul(getVipPrice(isVip, list.get(index), tem),
                                list.get(index).getAgoods_count());
                        judgeIntegralAndShow(p3, index, tAllPrice);
                        coupon.setText(context.getString(R.string.collectingAdapter15) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter16) +
                                doubleAndMul(aCache.getAsString("manual_max_amount"), list.get(index).getAgoods_count() + ""));
                    }
                }
                util.refresh();
            }
        };

        discount.addTextChangedListener(textWatcher);
        discount.setText(goods.getSku_discount()+"");
        discount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.util(index,7);
            }
        });

        final LinearLayout add = viewHolder.collectingSilverSelectedAdapterReduce;
        add.setOnClickListener(v -> {
            if ("1".equals(list.get(index).getSerial_number_state())) {
                Toast.makeText(context, "串碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                return;
            }else if (!"".equals(list.get(index).getCheng_price())){
                Toast.makeText(context, "條碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                return;
            }
            util.util(index, 1);
        });
        viewHolder.itemEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.util(index, 5);
            }
        });
        if(list.get(position).getSkujson()!=null && !list.get(position).getSkujson().isEmpty()){
            viewHolder.itemEdit.setVisibility(View.VISIBLE);
        }else {
            viewHolder.itemEdit.setVisibility(View.GONE);
        }
        final LinearLayout reduce = viewHolder.collectingSilverSelectedAdapterPlus;
        reduce.setOnClickListener(v -> {
            if ("1".equals(list.get(index).getSerial_number_state())) {
                Toast.makeText(context, "串碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                return;
            }else if (!"".equals(list.get(index).getCheng_price())){
                Toast.makeText(context, "條碼商品無法修改數量", Toast.LENGTH_SHORT).show();
                return;
            }
            util.util(index, 2);
        });
        viewHolder.itemCreditUserMenu.setOnClickListener(v -> {
            SlideLayout slideLayout = (SlideLayout) v.getParent();
            slideLayout.closeMenu(); //解決刪除item後下一個item變成open狀態問題
            util.util(index, 4);
//            Toast.makeText(parent.getContext(), "點擊了第" + position + "個的刪除", Toast.LENGTH_SHORT).show();
        });
        final TextView chooseSerial = viewHolder.collectingSilverSelectedSerial;
        chooseSerial.setOnClickListener(v -> util.util(index, 3));

        SlideLayout slideLayout = (SlideLayout) convertView;
        slideLayout.setOnStateChangeListener(new MyOnStateChangeListener());
        return convertView;
    }

    private void judgeIntegralAndShow(double p3, int index, TextView tAllPrice) {
        if (list.get(index).getTemporaryIntegral() > 0) {
            setAllPriceText(tAllPrice, "小計:$" + p3 + "+" + list.get(index).getTemporaryIntegral() + "積分");
        } else {
            setAllPriceText(tAllPrice, "小計:$" + p3);
        }
    }

    TextWatcher textWatcher = null;


    /**
     * 獲取兩數相乘的數
     */
    private double doubleAndMul(String retail_price, String vipDiscount) {
        return MathUtil.round(MathUtil.mul(Double.parseDouble(retail_price),
                Double.parseDouble(vipDiscount)), 2);
    }


    /**
     * 給textView控件設置數據
     */
    private void setAllPriceText(TextView orderSheetChooseAllPrice, String s) {
        orderSheetChooseAllPrice.setText(s);
    }


    /**
     * 條件是否成立
     */
    private boolean isEqual(String s, String plus_type) {
        return s.equals(plus_type);
    }


    /**
     * 是否是規格商品
     */
    private boolean isSkuGood(int position) {
        return list.get(position).getSkujson() != null &&
                list.get(position).getSkujson().size() > 0 &&
                isEqual("2", list.get(position).getAttribute_state());
    }

    /**
     * 獲取會員價格
     */
    private double getVipPrice(int type, GoodBean.DataBean.GoodsBean position, String retail_price) {//type 0:普通用戶 1:會員
        double price = 0;
        if ("1".equals(aCache.getAsString("store_manual_discount"))) {
            if (type == 1 && "1".equals(position.getVip_retail_state()) && ("2".equals(position.getPlus_mem_status()) ||
                    "1".equals(position.getPlus_mem_status()) && !"2".equals(infoBean.getPlus_type()))) {
                price = MathUtil.mul(Double.parseDouble(retail_price), infoBean.getDiscount());
            } else {
                price = Double.parseDouble(retail_price);
            }
            price = doubleAndMul(price + "", position.getSku_discount() + "");
        } else {
            if (position.getSku_discount() == 1) {
                if (type == 1 && "1".equals(position.getVip_retail_state()) && ("2".equals(position.getPlus_mem_status()) ||
                        "1".equals(position.getPlus_mem_status()) && !"2".equals(infoBean.getPlus_type()))) {
                    price = MathUtil.mul(Double.parseDouble(retail_price), infoBean.getDiscount());
                } else {
                    price = Double.parseDouble(retail_price);
                }
            } else {
                price = doubleAndMul(retail_price, position.getSku_discount() + "");
            }
        }
        if (position.getIs_gift_good() == 0) {
            if (price < 0.01) {
                price = 0.01;
            }
        }
        return price;
    }

    public interface Util {
        void util(int index, int type);//前一個參數：點擊的第幾個，第二個參數：點擊的哪個按鈕

        void refresh();
    }

    public void onclick(Util util) {
        this.util = util;
    }

    public SlideLayout slideLayout = null;

    class MyOnStateChangeListener implements SlideLayout.OnStateChangeListener {
        /**
         * 滑動後每次手勢擡起保證只有一個item是open狀態，加入sets集合中
         **/
        @Override
        public void onOpen(SlideLayout layout) {
            slideLayout = layout;
            if (sets.size() > 0) {
                for (SlideLayout s : sets) {
                    s.closeMenu();
                    sets.remove(s);
                }
            }
            sets.add(layout);
        }

        @Override
        public void onMove(SlideLayout layout) {
            if (slideLayout != null && slideLayout != layout) {
                slideLayout.closeMenu();
            }
        }

        @Override
        public void onClose(SlideLayout layout) {
            if (sets.size() > 0) {
                sets.remove(layout);
            }
            if (slideLayout == layout) {
                slideLayout = null;
            }
        }
    }

    static class ViewHolder {
        @BindView(R.id.collectingSilverSelected_adapter_name)
        TextView collectingSilverSelectedAdapterName;
        @BindView(R.id.collectingSilverSelected_adapter_give)
        ImageView collectingSilverSelectedAdapterGive;
        @BindView(R.id.collectingSilverSelected_adapter_price)
        TextView collectingSilverSelectedAdapterPrice;
        @BindView(R.id.collectingSilverSelected_adapter_costPrice)
        TextView collectingSilverSelectedAdapterCostPrice;
        @BindView(R.id.collectingSilverSelected_adapter_vipDiscount)
        TextView collectingSilverSelectedAdapterVipDiscount;
        @BindView(R.id.collectingSilverSelected_adapter_couponTip)
        TextView collectingSilverSelectedAdapterCouponTip;
        @BindView(R.id.collectingSilverSelected_adapter_discount)
        TextView collectingSilverSelectedAdapterDiscount;
        @BindView(R.id.collectingSilverSelected_adapter_tip)
        TextView collectingSilverSelectedAdapterTip;
        @BindView(R.id.collectingSilverSelected_adapter_total)
        TextView collectingSilverSelectedAdapterTotal;
        @BindView(R.id.collectingSilverSelected_serial)
        TextView collectingSilverSelectedSerial;
        @BindView(R.id.collectingSilverSelected_adapter_plus)
        LinearLayout collectingSilverSelectedAdapterPlus;
        @BindView(R.id.collectingSilverSelected_adapter_num)
        TextView collectingSilverSelectedAdapterNum;
        @BindView(R.id.collectingSilverSelected_adapter_reduce)
        LinearLayout collectingSilverSelectedAdapterReduce;
        @BindView(R.id.itemCreditUser_main)
        LinearLayout itemCreditUserMain;
        @BindView(R.id.itemCreditUser_menu)
        LinearLayout itemCreditUserMenu;
        @BindView(R.id.collecting_item_edit)
        ImageView itemEdit;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 是否是規格商品
     */
    private boolean isSkuGood(GoodBean.DataBean.GoodsBean goods) {
        return goods.getSkujson() != null &&
                goods.getSkujson().size() > 0 &&
                isEqual("2", goods.getAttribute_state());
    }

    public double getPrice(GoodBean.DataBean.GoodsBean goods){
        String originalPrice = "0";
        double temp_price = 0;
        double price = 0;
        int integral = 0;
        if (0 == goods.getIs_gift_good()) {
            if (!"".equals(goods.getCheng_price())){
                if (isVip == 1) {
                    price=getVipPrice(1,goods,goods.getCheng_price());
                    originalPrice = goods.getCheng_price();
                }else {
                    price=getVipPrice(0,goods,goods.getCheng_price());
                    originalPrice = goods.getCheng_price();
                }
            }else {
                if (isVip == 1) {
                    if ("2".equals(infoBean.getPlus_type()) && "1".equals(goods.getPlus_mem_status())) { //plus會員
                        if (isSkuGood(goods)) { //判斷規格商品
                            if (!TextUtils.isEmpty(goods.getSkujson().get(0).getPlus_price())) {
                                price = getVipPrice(1, goods, goods.getSkujson().get(0).getPlus_price());
                                originalPrice = goods.getSkujson().get(0).getPlus_price();
                                if (!TextUtils.isEmpty(goods.getSkujson().get(0).getPlus_mem_integral())) {
                                    integral = Integer.parseInt(goods.getSkujson().get(0).getPlus_mem_integral());
                                }
                            } else {
                                price = getVipPrice(1, goods, goods.getSkujson().get(0).getRetail_price());
                                originalPrice = goods.getSkujson().get(0).getRetail_price();
                            }
                        } else {
                            price = getVipPrice(1, goods, goods.getPlus_price());
                            originalPrice = goods.getPlus_price();
                            if (!TextUtils.isEmpty(goods.getPlus_mem_integral())) {
                                integral = Integer.parseInt(goods.getPlus_mem_integral());
                            }
                        }
                    } else { //常規會員
                        if ("1".equals(goods.getVip_retail_state())) {
                            if (isSkuGood(goods)) {
                                if ("2".equals(goods.getSkujson().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (goods.getSkujson().get(0).getStore_mem_price() != null &&
                                            !TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkujson().get(0).getStore_mem_price());
                                        originalPrice = goods.getSkujson().get(0).getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getSkujson().get(0).getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getStore_mem_price());
                                        originalPrice = goods.getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            }
                        } else {
                            if (isSkuGood(goods)) {
                                if ("2".equals(goods.getSkujson().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (goods.getSkujson().get(0).getStore_mem_price() != null &&
                                            !TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkujson().get(0).getStore_mem_price());
                                        originalPrice = goods.getSkujson().get(0).getStore_mem_price();
                                        if (!TextUtils.isEmpty(goods.getSkujson().get(0).getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getSkujson().get(0).getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                    originalPrice = goods.getRetail_price();
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = Double.parseDouble(goods.getStore_mem_price());
                                        originalPrice = price + "";
                                        price = getVipPrice(1, goods, price + "");
                                        if (!TextUtils.isEmpty(goods.getStore_mem_integral())) {
                                            integral = Integer.parseInt(goods.getStore_mem_integral());
                                        }
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                        originalPrice = goods.getRetail_price();
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (isSkuGood(goods)) {
                        if (goods.getSkujson().get(0).getRetail_price() != null &&
                                !TextUtils.isEmpty(goods.getSkujson().get(0).getRetail_price())) {
                            price = getVipPrice(0, goods, goods.getSkujson().get(0).getRetail_price());
                            originalPrice = goods.getSkujson().get(0).getRetail_price();
                        } else {
                            price = getVipPrice(0, goods, goods.getRetail_price());
                            originalPrice = goods.getRetail_price();
                        }
                    } else {
                        price = getVipPrice(0, goods, goods.getRetail_price());
                        originalPrice = goods.getRetail_price();
                    }
                }
            }
        }
        return MathUtil.mul(price, goods.getAgoods_count());
    }
    public HashMap<String,Object> getDiscountValue(GoodBean.DataBean.GoodsBean item, String value){
        String dis = value.trim();
        String tem = item.getTemporaryPrice();
        double skuDiscount=0.0;
        if (!TextUtils.isEmpty(dis) &&
                Double.parseDouble(dis) > 0) {
            skuDiscount=Double.parseDouble(dis);
        }
        if (TextUtils.isEmpty(dis)) {
            skuDiscount=1;
        }
        boolean a = skuDiscount < Double.parseDouble(aCache.getAsString("manual_min_discount")) ||
                skuDiscount > 1;
        double p1 = doubleAndMul(tem, "" + item.getAgoods_count());
        double p2 = MathUtil.mul(doubleAndMul(tem, skuDiscount + ""),item.getAgoods_count());
        HashMap<String,Object> map=new HashMap<>();
        if (a || MathUtil.sub(p1, p2) > doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + "")) {
            //tip.setText(R.string.collectingAdapter11);
//                    aCache.put("placeAnOrder", "0");//是否可以下單 1:可以 0:不可以
            map.put("isSucess",false);
            map.put("msg",context.getResources().getString(R.string.collectingAdapter11));
            map.put("discountAmount",MathUtil.sub(p1, p2));
            map.put("discountAmountNew",doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + ""));
            //coupon.setText(context.getString(R.string.collectingAdapter12) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter13) +
            //        doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + ""));
            double p3 = MathUtil.mul(getVipPrice(isVip,item, tem),
                    item.getAgoods_count());
            map.put("countAmount",p3);
            map.put("value",context.getString(R.string.collectingAdapter12) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter13) +
                           doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + ""));
            //judgeIntegralAndShow(p3, index, tAllPrice);
        } else {
            if (TextUtils.isEmpty(dis)) {
                map.put("isSucess",true);
                map.put("msg","");
                map.put("discountAmount",0.0);
                map.put("discountAmountNew",doubleAndMul(aCache.getAsString("manual_max_amount"),
                        item.getAgoods_count() + ""));
                map.put("value",context.getString(R.string.collectingAdapter14) +
                        doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + ""));
                double p3 = MathUtil.mul(getVipPrice(isVip,item, tem),
                        item.getAgoods_count());
                map.put("countAmount",p3);
            } else {
                double p3 = MathUtil.mul(getVipPrice(isVip,item, tem),
                        item.getAgoods_count());
                map.put("isSucess",true);
                map.put("msg","");
                map.put("discountAmount",MathUtil.sub(p1, p2));
                map.put("discountAmountNew",doubleAndMul(aCache.getAsString("manual_max_amount"),
                        item.getAgoods_count() + ""));
                map.put("value",context.getString(R.string.collectingAdapter15) + MathUtil.sub(p1, p2) + context.getString(R.string.collectingAdapter16) +
                        doubleAndMul(aCache.getAsString("manual_max_amount"), item.getAgoods_count() + ""));
                map.put("countAmount",p3);
            }
        }
        return map;
    }
}
